package com.runningboy.cluster.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @Description: 创建生产者，并生产消息发送消息中间件activeMq
 * @Author Runningboy
 * @Date 2020/7/25 16:48
 * @Version 1.0
 */
public class ProducerCluster {
    //failvoer：activemq的失效转移机制,randomize=true表示从中随机选择一个节点
    private static final String mqUrl = "failover:(tcp:// 192.168.0.101:61617,tcp:// 192.168.0.101:61618)?randomize=true";//activeMq默认的tcp端口是61616
    private static final String queueName = "queue-test";

    public static void main(String[] args) throws JMSException {
        //1、创建ConnectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        //2、创建Connetion
        Connection connection = connectionFactory.createConnection();
        //3、启动连接(切记要在使用结束时关闭连接)
        connection.start();
        //4、创建会话
        Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);//session自动应答

        //5、创建一个目标
        Destination destination = session.createQueue(queueName);

        //6、创建一个生产者
        MessageProducer producer = session.createProducer(destination);

        for (int i =0;i<100;i++){
            //7、创建消息
            TextMessage textMessage = session.createTextMessage("queueTest"+i);
            //8、发布消息到中间件
            producer.send(textMessage);
            System.out.println("生产消息："+textMessage);
        }
        //9、关闭连接
        connection.close();
    }
}
